package com.prayerdiary.fragments;

import java.util.Calendar;
import java.util.Date;

import android.app.DatePickerDialog;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Toast;

import com.prayerdiary.app.R;
import com.prayerdiary.database.PrayerDiaryDBHelper;
import com.prayerdiary.utils.Utils;

public class AddQazaDateFragment extends Fragment implements OnClickListener
{
	private Date qazaDayDate = null;

	@Override
	public void onActivityCreated(Bundle savedInstanceState)
	{
		super.onActivityCreated(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
	{
		View view = inflater.inflate(R.layout.add_qaza_date, container, false);
		addButtonListeners(view);
		return view;
	}

	private void addButtonListeners(View view)
	{
		Button addDateBtn = (Button) view.findViewById(R.id.aqd_add_date_btn);
		addDateBtn.setOnClickListener(this);

		Button datePickerBtn = (Button) view.findViewById(R.id.aqd_date_picker_btn);
		datePickerBtn.setOnClickListener(this);
		
	}

	@Override
	public void onClick(View v)
	{
		if (v.getId() == R.id.aqd_date_picker_btn)
		{
			
			showDatePickerDialog(v);
		}
		else if (v.getId() == R.id.aqd_add_date_btn)
		{
			addQazaDate(v);
		}
	}

	/**
	 * Display the date picker dialog
	 */
	private void showDatePickerDialog(final View v)
	{
		final Calendar c = Calendar.getInstance();
		int mYear = c.get(Calendar.YEAR);
		int mMonth = c.get(Calendar.MONTH);
		int mDay = c.get(Calendar.DAY_OF_MONTH);

		DatePickerDialog dpd = new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener()
		{
			@Override
			public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
			{
				Calendar calendar = Calendar.getInstance();
				calendar.clear();
				calendar.set(year, (monthOfYear), dayOfMonth, 0, 0, 0);
				qazaDayDate = calendar.getTime();
				EditText dateET = (EditText) getActivity().findViewById(R.id.aqd_date_et);
				dateET.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year);
			}
		}, mYear, mMonth, mDay);
		dpd.show();
	}

	private void addQazaDate(View v)
	{
		if (qazaDayDate != null)
		{
			PrayerDiaryDBHelper dbHelper = new PrayerDiaryDBHelper(getActivity());
			boolean inserted = dbHelper.addQazaDay(qazaDayDate);
			if (!inserted)
			{
				Utils u = new Utils();
				u.showMessage("Sorry", "This date has already been saved", getActivity()).run();
				qazaDayDate = null;
				EditText text = (EditText) getActivity().findViewById(R.id.aqd_date_et);
				text.setText(null);
			}
			else
			{
				Toast.makeText(getActivity().getApplicationContext(), "Date Inserted", Toast.LENGTH_SHORT).show();
				qazaDayDate = null;
				EditText text = (EditText) getActivity().findViewById(R.id.aqd_date_et);
				text.setText(null);
			}
		}
		else
		{
			Utils u = new Utils();
			u.showMessage("Error", "Please select a date", getActivity()).run();
		}
	}
}